package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import dao.TruckInfo_Repository;
import play.data.FormFactory;
import play.libs.Json;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;
import java.util.concurrent.CompletionStage;


/**
 * The controller keeps all database operations behind the repository, and uses
 * {@link play.libs.concurrent.HttpExecutionContext} to provide access to the
 * {@link play.mvc.Http.Context} methods like {@code request()} and {@code flash()}.
 */
public class TruckInfoController extends Controller {

    private final FormFactory formFactory;
    private final TruckInfo_Repository tTruckInfoRepository;
    private final HttpExecutionContext ec;

    @Inject
    public TruckInfoController(FormFactory formFactory, TruckInfo_Repository tTruckInfoRepository, HttpExecutionContext ec) {
        this.formFactory = formFactory;
        this.tTruckInfoRepository = tTruckInfoRepository;
        this.ec = ec;
    }
/*   
    public CompletionStage<Result> getDeptByID() {
		JsonNode json = request().body().asJson();
		Integer DeptID = json.findPath("DeptID").intValue();
	    return tDeptInfoRepository.getDeptByID(DeptID).thenApplyAsync(_DeptInfo -> {
	    	return ok(Json.toJson(_DeptInfo));
	    }, ec.current());
    }
*/    
    public CompletionStage<Result> getTruckList() {
		JsonNode json = request().body().asJson();
        Integer GPSFlag = json.findPath("GPSFlag").intValue();

	    return tTruckInfoRepository.getTruckList(GPSFlag).thenApplyAsync(_getTruckList -> {
	    	return ok(Json.toJson(_getTruckList));
	    }, ec.current());
    }

    public CompletionStage<Result> boundTruckAndQRcode() {
        JsonNode json = request().body().asJson();
        String truckNumber = json.findPath("truckNumber").textValue();
        String GPSId = json.findPath("GPSId").textValue();
        String SimNo = json.findPath("SimNo").textValue();
        String Industry= json.findPath("Industry").textValue();
        String phoneNumber= json.findPath("phoneNumber").textValue();
        String  latitude =json.findPath("latitude").textValue();
        String  longitude =json.findPath("longitude").textValue();
        return tTruckInfoRepository.boundTruckAndQRcode(truckNumber,GPSId,SimNo,Industry,phoneNumber,latitude,longitude).thenApplyAsync(_getTruckList -> {
            return ok(Json.toJson(_getTruckList));
        }, ec.current());
    }
    public CompletionStage<Result> getTruckPlace() {
        JsonNode json = request().body().asJson();
        String truckNumber = json.findPath("truckNumber").textValue();

        return tTruckInfoRepository.getTruckPlace(truckNumber).thenApplyAsync(_getTruckList -> {
            return ok(Json.toJson(_getTruckList));
        }, ec.current());
    }



}
